package com.woniuxy.service.impl;

import com.woniuxy.dao.MessageDao;
import com.woniuxy.dao.SigningDao;
import com.woniuxy.entity.Coach;
import com.woniuxy.entity.Message;
import com.woniuxy.entity.Signing;
import com.woniuxy.entity.Venue;
import com.woniuxy.service.MessageService;
import com.woniuxy.service.SigningService;
import com.woniuxy.utils.CodeUtils;
import com.woniuxy.utils.LoginRegister;
import com.woniuxy.utils.TimeUtils;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Transactional
@Data
@Service
public class SigningServiceImpl implements SigningService {

    @Autowired
    private SigningDao signingDao;

    @Autowired
    private MessageDao messageDao;

    /**
     * 根据教练ID查询他签约了哪些场馆
     * @param coachId
     * @return
     */
    @Override
    public List<Venue> findVenuesByCoachId(int coachId) {
        return signingDao.findVenuesByCoachId(coachId);
    }

    /**
     * 根据场馆id查询签约的教练有哪些
     * @param venueId 场馆id
     * @return 签约的所有教练
     */
    @Override
    public List<Coach> findCoachesByVenueId(int venueId) {
        return signingDao.findCoachesByVenueId(venueId);
    }

    /**
     * 通过场馆id和教练id查询是否签约
     * @param cid
     * @param vid
     * @return 是否有数据
     */
	@Override
	public int isSigning(int cid, int vid) {
		if(signingDao.isSigning(cid, vid)==null) {
			//不存在返回1
			return 1;
		}else {
			return 0;
		}
	}

    /**
     * 删除签约关系
     * @param signing
     * @return
     */
    @Override
    public int fireCoach(Signing signing) {
        int i=messageDao.addNewMessage(new Message().setTarget(signing.getCid()).setSender(signing.getVid()).setPurpose("3")
                .setSendTime(TimeUtils.getNowTime()).setContent("经过慎重的考虑，我们场馆决定与你解约"));
        return signingDao.fireCoach(signing)>0&&i>0? LoginRegister.OPERATION_SUCCESS:LoginRegister.OPERATION_FAILED;
    }

    /**
     * 新增签约
     * @param signing 封装的cid和vid
     * @return
     */
    @Override
    public int addNewSigning(Signing signing) {
        if (isSigning(signing.getCid(),signing.getVid())==0){
            return CodeUtils.SIGNING_EXIST;
        }
        signing.setSignTime(TimeUtils.getNowTime());
        return signingDao.addNewSigning(signing)>0?LoginRegister.OPERATION_SUCCESS:LoginRegister.OPERATION_FAILED;
    }
}
